package com.sp.scmdb.daoImpl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.sp.scmdb.bean.SecurityCommentedBean;
import com.sp.scmdb.common.dao.BaseDaoSupport;
import com.sp.scmdb.dao.SecurityCommentedDao;

@Repository
public class SecurityCommentedDaoImpl extends BaseDaoSupport implements SecurityCommentedDao {

	@Override
	public List<SecurityCommentedBean> getResultList(String beginDate, String endDate) {
		
		List<SecurityCommentedBean> result = new ArrayList<SecurityCommentedBean>();
		
		Connection con = getConnection();
		CallableStatement call = null;
		ResultSet res = null;
		try {
			call = con.prepareCall("{call proc_ParticipateCommented(?, ?)}");
			call.setString(1, beginDate);
			call.setString(2, endDate);
			res = call.executeQuery();
			
			ResultSetMetaData rsmd = res.getMetaData();
			int columnLength = rsmd.getColumnCount();
			while(res.next()) {
				SecurityCommentedBean bean = new SecurityCommentedBean();
				bean.setEmpId(res.getInt("id"));
				bean.setEmpName(res.getString("empName"));
				
				List<String> columnList = new ArrayList<String>();
				for(int j = 4; j <= columnLength; j++) {
					columnList.add(res.getString(rsmd.getColumnName(j)));
				}
				bean.setColumns(columnList);
					
				result.add(bean);
			}
		} catch (SQLException e) {
			throw new IllegalArgumentException(e.getMessage());
		} finally {
			if(con != null) {
				try {
					if(!con.isClosed()) {
						con.close();
					}
				} catch (SQLException e) {
				}
			}
		}
		
		return result;
	}
}
